package com.school.gift.controller.admin;

import com.github.pagehelper.PageInfo;
import com.school.gift.domain.User;
import com.school.gift.service.UserService;
import com.school.support.base.AdminBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import zhibi.common.support.base.Page;
import zhibi.common.support.exception.MessageException;
import zhibi.common.support.mybatis.example.Example;
import zhibi.common.support.mybatis.example.ExampleType;
import zhibi.common.support.mybatis.example.ExampleUtil;
import zhibi.common.support.response.Response;

import java.util.Date;
import java.util.List;

/**
 * Created by 执笔 on 2017/3/22.
 */
@Controller
@RequestMapping("admin/user")
public class MUserController extends AdminBaseController {

    @Autowired
    private UserService userService;


    @RequestMapping("list")
    public String list(Model model, Page page, User user) {
        Example example = ExampleUtil.getInstance()
                .addParam("username", user.getUsername(), ExampleType.Operation.LIKE)
                .addParam("phone", user.getPhone(), ExampleType.Operation.LIKE)
                .addOrder("addtime")
                .getExample();
        PageInfo<User> pageInfo = userService.selectByExample(example, page);
        setModelAttribute(model, pageInfo);
        return "admin/user/list";
    }

    @RequestMapping("updateStatus/{id}")
    public String updateStatus(@PathVariable Long id) {
        User user = userService.selectByPK(id);
        if (null == user) {
            throw new MessageException("该用户不存在");
        }
        user.setStatus((user.getStatus() + 1) % 2);
        userService.updateByIdSelective(user);
        return refresh();
    }

    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        User user = userService.selectByPK(id);
        if (null == user) {
            throw new MessageException("该用户不存在");
        }
        model.addAttribute(user);
        return "admin/user/detail";
    }

}
